<?php

declare(strict_types=1);

namespace App\Robot\Amqp\Consumer;

use App\CustHub\Constants\ContactTaskCenterStatusConstants;
use App\CustHub\Constants\ContactTaskCenterTyperConstants;
use App\CustHub\Constants\CrowSendingConstants;
use App\CustHub\Model\ChContact;
use App\CustHub\Model\ChContactTaskCenter;
use App\CustHub\Model\ChCrowdInvite;
use App\CustHub\Service\ChSendRecordService;
use App\CustHub\Service\CrowdInviteService;
use App\Robot\Job\CrowdInviteJob;
use App\Robot\Job\DelayedTaskJob;
use App\Robot\Job\SendRecordJob;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use PhpAmqpLib\Message\AMQPMessage;
use function Hyperf\Collection\data_get;
use function Hyperf\Coroutine\co;

#[Consumer(exchange:"crowd_invite", routingKey:"crowd_invite", queue:"crowd_invite", name:"CrowdInvite", nums:1)]
class CrowdInviteConsumer extends ConsumerMessage
{
    public function consumeMessage($data, AMQPMessage $message): Result
    {
        // 判断是否需要延迟
        $delay = data_get($data,'delay',0);
        $data = data_get($data,'data');
        $schemeId = $data['schemeId'];
        $subSchemeId = $data['subSchemeId'];
        $robotId = $data['robotId'];
        $customId = $data['customId'];
        $isLastCustom = $data['isLastCustom'];

        if($delay > 0){
            $driver = container()->get(DriverFactory::class)->get('low_priority');
            $job = new CrowdInviteJob($data);
            $driver->push($job,(int)$delay);
            return Result::ACK;
        }
        var_dump('群邀请任务处理中...');
        $crowdInvite = ChCrowdInvite::query() -> where('id',$schemeId) -> first();
        if(empty($crowdInvite)){
            return Result::ACK;
        }
        $roomCnf = $crowdInvite->crowd_conf;
        $roomCnf['invite_guide_switch'] = $crowdInvite->is_guide;
        $roomCnf['guide_text'] = $crowdInvite->content;
        if($crowdInvite-> status == CrowSendingConstants::SENDING){
            container()->get(CrowdInviteService::class)->triggerInvite($schemeId, $robotId, $customId, $roomCnf,$subSchemeId,$isLastCustom);
        }
        return Result::ACK;
    }


    public function isEnable(): bool
    {
        return \Hyperf\Support\env('AMQP_CONSUMER_ENABLE',false);
    }
}